package com.fingard.dsp.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import com.fingard.FGBiz;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;

/**任务执行类*/
public class TaskRunner {
	
	private static AtomicInteger integerID = new AtomicInteger(0);
	private int groupSize = 1;//所在线程池任务组的个数
	
	public TaskRunner(int pGroupSize){
		groupSize = pGroupSize;
	}
	
	public void runTask(XElement gXe){
		String logCode = "TaskRunner";
		try {
			int tmpIntID = integerID.incrementAndGet();
            if (tmpIntID >= Integer.MAX_VALUE - 100) {
                integerID = new AtomicInteger(0);
                tmpIntID = integerID.incrementAndGet();
            }
            try {
            	int remainder = tmpIntID%(20*groupSize);
            	String gName = gXe.getSingleText("name");
            	if(remainder>=1 && remainder<=groupSize){
            		FGBiz.logTrace.info(logCode, "开始执行“"+gName+"”\t"+remainder);
            	}
            	ArrayList<XElement> taskList = gXe.getChildList("task");
            	if(taskList!=null && taskList.size()>0){
            		for(XElement eachXe : taskList){
            			String tName = eachXe.getSingleText("name");
            			String command = eachXe.getSingleText("command");
            			if(remainder>=1 && remainder<=groupSize){
            				FGBiz.logTrace.info(logCode, "开始执行“"+gName+"”-“"+tName+"”-“"+command+"”\t"+remainder);
            			}
            			if(StringHelper.hasAnyChar(command)){
            				String className = "com.fingard.dsp.task.command."+command;
        					Class<?> cmdClass = FGBiz.javaClassMap.getJavaClass(className.toLowerCase());
        					if (cmdClass == null) {
        						cmdClass = Class.forName(className);
        					}
        					if (cmdClass != null) {
        						TaskBase tBase=(TaskBase)cmdClass.newInstance();
        						tBase.taskName = tName;
        						tBase.logID = tmpIntID;
        						HashMap<String,String> params = new HashMap<String,String>();
        						XElement paramList = eachXe.getSingleChild("paramList");
        						if(paramList != null){
        							ArrayList<XElement> xeParams = paramList.getAllChildList();
        							if(xeParams != null && xeParams.size()>0){
        								for(XElement eachPm : xeParams){
        									params.put(eachPm.name.toLowerCase(), eachPm.innerText);
        								}
        							}
        						}
        						ITask iTask = (ITask)tBase;
        						iTask.execTask(params);
        					}else{
        						if(remainder==1){
        							FGBiz.logTrace.error(logCode, "不支持的命令："+command+"\t"+remainder);
        						}
        					}
            			}else{
            				if(remainder==1){
            					FGBiz.logTrace.error(logCode, gName+"-"+tName+"的命令为空\t"+remainder);
            				}
            			}
            		}
            	}else{
            		if(remainder==1){
            			FGBiz.logTrace.error(logCode, gName+"的任务列表为空\t"+remainder);
            		}
            	}
            	
            }catch (Throwable t) {
            	FGBiz.logTrace.exception(logCode, t, tmpIntID+"异常");
            }
		} catch (Exception ex) {
        	FGBiz.logTrace.exception(logCode, ex);
            
        } catch (Throwable t2) {
        	FGBiz.logTrace.exception(logCode, t2);
        }
	}
}
